﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;

namespace LowLevelGraphics.ImageFormats
{
    /// <summary>
    /// 
    /// </summary>
    public class KnownImageFormat
    {
        private object m_ImageFormat = null;

        /// <summary>
        /// 
        /// </summary>
        public KnownImageFormat()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public KnownImageFormat(ImageFormat _imageFormat)
        {
            m_ImageFormat = _imageFormat;
        }

        public KnownImageFormat(AbstractImageFormat _imageFormat)
        {
            m_ImageFormat = _imageFormat;
        }

        /// <summary>
        /// Save
        /// </summary>
        /// <returns></returns>
        public bool Save(string _sFileName, UnsafeBitmap _bitmap)
        {
            ImageFormat imageFormat = m_ImageFormat as ImageFormat;
            if (imageFormat != null)
            {
                _bitmap.InternalBitmap.Save(_sFileName, imageFormat);
                return true;
            }
            
            bool bSaved = false;
            AbstractImageFormat abstractImageFormat = m_ImageFormat as AbstractImageFormat;
            if (abstractImageFormat != null)
            {
                bSaved = abstractImageFormat.Save(_sFileName);
            }
            return bSaved;
        }
    }

    public class KnownImageFormats
    {
        Dictionary<string, KnownImageFormat> m_aKnownImageFormat = new Dictionary<string, KnownImageFormat>();

        public KnownImageFormats()
        {
            m_aKnownImageFormat.Add("bmp", new KnownImageFormat(ImageFormat.Bmp));
            m_aKnownImageFormat.Add("emf", new KnownImageFormat(ImageFormat.Emf));
            m_aKnownImageFormat.Add("gif", new KnownImageFormat(ImageFormat.Gif));
            m_aKnownImageFormat.Add("ico", new KnownImageFormat(ImageFormat.Icon));
            //m_aKnownImageFormat.Add("jp2", new KnownImageFormat());
            m_aKnownImageFormat.Add("jpg", new KnownImageFormat(ImageFormat.Jpeg));
            m_aKnownImageFormat.Add("png", new KnownImageFormat(ImageFormat.Png));
            m_aKnownImageFormat.Add("tif", new KnownImageFormat(ImageFormat.Tiff));
            m_aKnownImageFormat.Add("tiff", new KnownImageFormat(ImageFormat.Tiff));
            m_aKnownImageFormat.Add("dicom", new KnownImageFormat(new Dicom()));
            m_aKnownImageFormat.Add("dxf", new KnownImageFormat(new Dxf()));
            //m_aKnownImageFormat.Add("pdf", new KnownImageFormat(new Pdf()));
            m_aKnownImageFormat.Add("psd", new KnownImageFormat(new PSD()));
            m_aKnownImageFormat.Add("svg", new KnownImageFormat(new SVGReader()));
            m_aKnownImageFormat.Add("hdr", new KnownImageFormat(new HDR()));
            m_aKnownImageFormat.Add("htm", new KnownImageFormat(new Html()));
            //m_aKnownImageFormat.Add("interfile", new KnownImageFormat(new Interfile()));
            //m_aKnownImageFormat.Add("MayoAnalyze", new KnownImageFormat(new MayoAnalyze()));
            //m_aKnownImageFormat.Add("PAM", new KnownImageFormat(new PAM()));
            //m_aKnownImageFormat.Add("nef", new KnownImageFormat());
            m_aKnownImageFormat.Add("pbm", new KnownImageFormat(new PBM()));
            m_aKnownImageFormat.Add("pcx", new KnownImageFormat(new PCXLoader()));
            m_aKnownImageFormat.Add("pgm", new KnownImageFormat(new PGM()));
            m_aKnownImageFormat.Add("ppm", new KnownImageFormat(new PPM()));
            m_aKnownImageFormat.Add("rtf", new KnownImageFormat(new Rtf()));
            m_aKnownImageFormat.Add("txt", new KnownImageFormat(new Txt()));
            m_aKnownImageFormat.Add("wmf", new KnownImageFormat(ImageFormat.Wmf));
            m_aKnownImageFormat.Add("tga", new KnownImageFormat(new TGA()));
        }

        /// <summary>
        /// Save pic
        /// </summary>
        /// <param name="_sFileName"></param>
        public void Save(string _sFileName, UnsafeBitmap _bitmap)
        {
            string sExtension = Path.GetExtension(_sFileName).Trim('.').ToLower();

            KnownImageFormat knownImageFormat = null;
            //Save as jpg if no type is given. 
            if (!this.m_aKnownImageFormat.ContainsKey(""))
            {
                knownImageFormat = this["jpg"];
            }
            else
            {
                knownImageFormat = this[sExtension];
            }

            knownImageFormat.Save(_sFileName, _bitmap);
        }

        /// <summary>
        /// Get ImageFormat by extension
        /// </summary>
        /// <param name="_sExtension"></param>
        /// <returns></returns>
        public KnownImageFormat this[string _sExtension]
        {
            get { return m_aKnownImageFormat[_sExtension]; }
        }
    }
}
